// pages/kind/index.js
import {
    getGoodsCate,
    getGoodsList,
    addCate
} from "../../utils/api"
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeKey: 0,
        goodsCategory: [],
        goodsList: [],
        cateList:{},
        count:1,
        flag:false
    },
    getcateList(categoryId) {
        getGoodsList({
            categoryId
        }).then(res => {
            console.log(res);
            if (res.code == "0") {
                this.setData({
                    goodsList: res.data.result
                })
            } else {
                this.setData({
                    goodsList: []
                })
            }
        })
    },
    getGoods(id) {
        getGoodsCate().then(res => {
            let idd = 0
            if (id) {
                idd = res.data.findIndex(item => {
                    return item.id == id
                })
            }else{
                this.getcateList(res.data[0].id)
            }
            this.data.goodsCategory = res.data
            this.setData({
                activeKey: idd,
                goodsCategory: this.data.goodsCategory
            })
        })
    },
    onChange(event) {
        console.log(event.detail);
        let cateid = this.data.goodsCategory[event.detail].id
        console.log(cateid);
        this.getcateList(cateid)
    },
    goGoodsDetail(e) {
        console.log(e.currentTarget.dataset.detail.id);
        const id = e.currentTarget.dataset.detail.id
        wx.navigateTo({
            url: `../goodsDetail/index?id=${id}`
        })
    },
    addCate(e){
        addCate({
            goodsId:e.currentTarget.dataset.id,
            number:this.data.count,
            token:wx.getStorageSync("token")
        }).then(res=>{
            console.log(res);   
            // Toast('添加成功');
        })
        this.setData({
            count:1,
            show:false
        })
    },
    pdCate(){

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        if (options.id) {
            console.log(111);
            this.getGoods(options.id)
            this.getcateList(options.id)
        } else {
            console.log(222);
            this.getGoods()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})